﻿Imports System.Data.OleDb
Public Class frmHangTraVe
    Private ReportType As Integer = 0
    Private IndexCurrentMonth As Integer = 0
    Private Sub frmHangTraVe_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            DisableControl()
            dtpTheoNgay.Enabled = True
            Nam_Load()
            Thang_Load()
            Report_TheoNgay()
            'Phân quyền Form
            Dim aPermission(4) As Boolean
            aPermission = PhanQuyenForm("MenuHangTraVe", frmMain.UserId)
            cmdAddNew.Enabled = aPermission(0)
            cmdEdit.Enabled = aPermission(1)
            cmdAproved.Enabled = aPermission(1)
            cmdDelete.Enabled = aPermission(2)
            cmdPrint.Enabled = aPermission(3)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#Region "Hàm Thêm, Sửa, Xóa"
    Private Sub cmdAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddNew.Click
        Try
            ShowModalForm(frmThemHangTraVe2, False, False)
            Select Case ReportType
                Case 0
                    Report_TheoNgay()
                Case 1
                    Report_TheoThang()
                Case 2
                    Report_TheoNam()
                Case 3
                    Report_TuNgayDenNgay()
            End Select
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Try
            Dim PhieuNhapKhoId As Integer = Integer.Parse(dgvPhieuNhapKho.Rows(dgvPhieuNhapKho.CurrentCell.RowIndex).Cells("PhieuNhapKhoId").Value.ToString)
            'kiem tra tinh trang
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim dbCommand As OleDbCommand
            Dim query As String = "Select TrangThai From PhieuNhapKho Where PhieuNhapKhoId=" & PhieuNhapKhoId
            dbCommand = New OleDbCommand(query, dbConn)
            Dim TrangThai As String = dbCommand.ExecuteScalar()
            dbConn.Close()
            If TrangThai = "Đã duyệt" Then
                MessageBox.Show("Phiếu đã duyệt.Bạn không thể chỉnh sửa thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            Else
                frmThemHangTraVe2.PhieuNhapKhoId = PhieuNhapKhoId
                ShowModalForm(frmThemHangTraVe2, False, False)
                frmThemHangTraVe2.PhieuNhapKhoId = -1
                Select Case ReportType
                    Case 0
                        Report_TheoNgay()
                    Case 1
                        Report_TheoThang()
                    Case 2
                        Report_TheoNam()
                    Case 3
                        Report_TuNgayDenNgay()
                End Select
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub cmdAproved_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAproved.Click
        Try
            Dim PhieuNhapKhoId As Integer = Integer.Parse(dgvPhieuNhapKho.Rows(dgvPhieuNhapKho.CurrentCell.RowIndex).Cells("PhieuNhapKhoId").Value.ToString)
            'kiem tra tinh trang
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim dbCommand As OleDbCommand
            Dim query As String = "Select TrangThai From PhieuNhapKho Where PhieuNhapKhoId=" & PhieuNhapKhoId
            dbCommand = New OleDbCommand(query, dbConn)
            Dim TrangThai As String = dbCommand.ExecuteScalar()
            If TrangThai = "Đã duyệt" Then
                MessageBox.Show("Phiếu đã duyệt.Bạn không thể duyệt được nữa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                dbConn.Close()
                Exit Sub
            Else
                Dim dlgRes As DialogResult
                dlgRes = MessageBox.Show( _
                      "Bạn muốn duyệt xác nhận phiếu nhập kho này." & vbCrLf & "Chú ý: Sau khi duyệt phiếu này bạn không thể chỉnh sửa thông tin phiếu nữa." & vbCrLf & "Thông tin nhập kho sẽ được cập nhật vào kho.", _
                      "Duyệt phiếu nhập kho", _
                MessageBoxButtons.OKCancel, _
                      MessageBoxIcon.Question)
                If dlgRes = DialogResult.OK Then
                    query = "Update PhieuNhapKho Set TrangThai=N'Đã duyệt' Where PhieuNhapKhoId=" & PhieuNhapKhoId & ";Update ChiTietPhieuNhapKho Set TrangThai=N'Đã duyệt' Where PhieuNhapKhoId=" & PhieuNhapKhoId
                    dbCommand = New OleDbCommand(query, dbConn)
                    dbCommand.ExecuteNonQuery()
                    'nhap kho
                    Dim dsPhieuNhapKho As New DataSet
                    Dim dtPhieuNhapKho As New DataTable
                    query = "Select * from ChiTietPhieuNhapKho Where PhieuNhapKhoId=" & PhieuNhapKhoId
                    Dim adapter As OleDb.OleDbDataAdapter
                    adapter = New OleDb.OleDbDataAdapter(query, dbConn)
                    adapter.Fill(dsPhieuNhapKho)
                    dtPhieuNhapKho = dsPhieuNhapKho.Tables(0)
                    Dim SoLuongThuc As Single
                    Dim IDSanPham As Integer
                    For i As Integer = 0 To dtPhieuNhapKho.Rows.Count - 1
                        SoLuongThuc = Single.Parse(dtPhieuNhapKho.Rows(i)("SoLuongThuc"))
                        IDSanPham = Integer.Parse(dtPhieuNhapKho.Rows(i)("IDSanPham"))
                        query = "Update SanPham Set SoLuongTon=SoLuongTon + " & SoLuongThuc & " Where IdSanPham=" & IDSanPham
                        dbCommand = New OleDbCommand(query, dbConn)
                        dbCommand.ExecuteNonQuery()
                    Next
                    dbConn.Close()
                    Select Case ReportType
                        Case 0
                            Report_TheoNgay()
                        Case 1
                            Report_TheoThang()
                        Case 2
                            Report_TheoNam()
                        Case 3
                            Report_TuNgayDenNgay()
                    End Select
                End If
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub rbtTatCa_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtTatCa.CheckedChanged
        Try
            Select Case ReportType
                Case 0
                    Report_TheoNgay()
                Case 1
                    Report_TheoThang()
                Case 2
                    Report_TheoNam()
                Case 3
                    Report_TuNgayDenNgay()
            End Select
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub rbtChuaDuyet_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtChuaDuyet.CheckedChanged
        Try
            Select Case ReportType
                Case 0
                    Report_TheoNgay()
                Case 1
                    Report_TheoThang()
                Case 2
                    Report_TheoNam()
                Case 3
                    Report_TuNgayDenNgay()
            End Select
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub rbtDaDuyet_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtDaDuyet.CheckedChanged
        Try
            Select Case ReportType
                Case 0
                    Report_TheoNgay()
                Case 1
                    Report_TheoThang()
                Case 2
                    Report_TheoNam()
                Case 3
                    Report_TuNgayDenNgay()
            End Select
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Load Phiếu nhập kho"
    Public Sub Report_TheoNgay()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsPhieuNhapKho As New DataSet
            Dim dtPhieuNhapKho As New DataTable
            Dim query As String = ""
            If rbtTatCa.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And NgayNhapKho='" & dtpTheoNgay.Value.ToShortDateString & "'"
            ElseIf rbtChuaDuyet.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And TrangThai=N'Chưa duyệt' And NgayNhapKho='" & dtpTheoNgay.Value.ToShortDateString & "'"
            ElseIf rbtDaDuyet.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And TrangThai=N'Đã duyệt' And NgayNhapKho='" & dtpTheoNgay.Value.ToShortDateString & "'"
            End If
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsPhieuNhapKho)
            dbConn.Close()
            dgvPhieuNhapKho.AutoGenerateColumns = False
            dtPhieuNhapKho = dsPhieuNhapKho.Tables(0)
            dtPhieuNhapKho.Columns.Add("STT")
            For i As Integer = 0 To dtPhieuNhapKho.Rows.Count - 1
                dtPhieuNhapKho.Rows(i)("STT") = i + 1
                formatDate2VN(dtPhieuNhapKho.Rows(i)("Ngay"))
                formatDate2VN(dtPhieuNhapKho.Rows(i)("NgayNhapKho"))
            Next
            dgvPhieuNhapKho.DataSource = dtPhieuNhapKho
            If dgvPhieuNhapKho.Rows.Count - 1 > 0 Then
                dgvPhieuNhapKho.Rows(0).Selected = True
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Public Sub Report_TheoThang()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsPhieuNhapKho As New DataSet
            Dim dtPhieuNhapKho As New DataTable
            Dim query As String = ""
            Dim Thang As MonthInfo
            Thang = CType(cbxTheoThang.SelectedItem, MonthInfo)
            If Thang Is Nothing Then
                MessageBox.Show("Bạn chưa chọn tháng.Vui lòng kiểm tra lại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If rbtTatCa.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And ThangNhap='" & Thang.TenThang & "'"
            ElseIf rbtChuaDuyet.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And TrangThai=N'Chưa duyệt' And ThangNhap='" & Thang.TenThang & "'"
            ElseIf rbtDaDuyet.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And TrangThai=N'Đã duyệt' And ThangNhap='" & Thang.TenThang & "'"
            End If
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsPhieuNhapKho)
            dbConn.Close()
            dgvPhieuNhapKho.AutoGenerateColumns = False
            dtPhieuNhapKho = dsPhieuNhapKho.Tables(0)
            dtPhieuNhapKho.Columns.Add("STT")
            For i As Integer = 0 To dtPhieuNhapKho.Rows.Count - 1
                dtPhieuNhapKho.Rows(i)("STT") = i + 1
                formatDate2VN(dtPhieuNhapKho.Rows(i)("Ngay"))
                formatDate2VN(dtPhieuNhapKho.Rows(i)("NgayNhapKho"))
            Next
            dgvPhieuNhapKho.DataSource = dtPhieuNhapKho
            If dgvPhieuNhapKho.Rows.Count - 1 > 0 Then
                dgvPhieuNhapKho.Rows(0).Selected = True
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Public Sub Report_TheoNam()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsPhieuNhapKho As New DataSet
            Dim dtPhieuNhapKho As New DataTable
            Dim query As String = ""
            Dim Nam As YearInfo
            Nam = CType(cbxTheoNam.SelectedItem, YearInfo)
            If Nam Is Nothing Then
                MessageBox.Show("Bạn chưa chọn năm.Vui lòng kiểm tra lại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
            If rbtTatCa.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And NamNhap='" & Nam.TenNam & "'"
            ElseIf rbtChuaDuyet.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And TrangThai=N'Chưa duyệt' And NamNhap='" & Nam.TenNam & "'"
            ElseIf rbtDaDuyet.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And TrangThai=N'Đã duyệt' And NamNhap='" & Nam.TenNam & "'"
            End If
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsPhieuNhapKho)
            dbConn.Close()
            dgvPhieuNhapKho.AutoGenerateColumns = False
            dtPhieuNhapKho = dsPhieuNhapKho.Tables(0)
            dtPhieuNhapKho.Columns.Add("STT")
            For i As Integer = 0 To dtPhieuNhapKho.Rows.Count - 1
                dtPhieuNhapKho.Rows(i)("STT") = i + 1
                formatDate2VN(dtPhieuNhapKho.Rows(i)("Ngay"))
                formatDate2VN(dtPhieuNhapKho.Rows(i)("NgayNhapKho"))
            Next
            dgvPhieuNhapKho.DataSource = dtPhieuNhapKho
            If dgvPhieuNhapKho.Rows.Count - 1 > 0 Then
                dgvPhieuNhapKho.Rows(0).Selected = True
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Public Sub Report_TuNgayDenNgay()
        Try
            Dim TuNgay As Integer = ConvertDateToInt(dtpTuNgay.Value.ToString.Split(" ")(0))
            Dim DenNgay As Integer = ConvertDateToInt(dtpDenNgay.Value.ToString.Split(" ")(0))
            If DenNgay < TuNgay Then
                Dim dlgRes As DialogResult
                dlgRes = MessageBox.Show( _
                          "Ngày đến không thể nhỏ hơn từ ngày.Vui lòng nhập lại", _
                          "Thông báo", _
                    MessageBoxButtons.OK, _
                          MessageBoxIcon.Question)
                If dlgRes = Windows.Forms.DialogResult.OK Then
                    Exit Sub
                End If
            End If

            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsPhieuNhapKho As New DataSet
            Dim dtPhieuNhapKho As New DataTable
            Dim query As String = ""
            If rbtTatCa.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And NgayNhapBangSo <=" & DenNgay & " And NgayNhapBangSo >=" & TuNgay & ""
            ElseIf rbtChuaDuyet.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And TrangThai=N'Chưa duyệt' And NgayNhapBangSo <=" & DenNgay & " And NgayNhapBangSo >=" & TuNgay & ""
            ElseIf rbtDaDuyet.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And TrangThai=N'Đã duyệt' And NgayNhapBangSo <=" & DenNgay & " And NgayNhapBangSo >=" & TuNgay & ""
            End If
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsPhieuNhapKho)
            dbConn.Close()
            dgvPhieuNhapKho.AutoGenerateColumns = False
            dtPhieuNhapKho = dsPhieuNhapKho.Tables(0)
            dtPhieuNhapKho.Columns.Add("STT")
            For i As Integer = 0 To dtPhieuNhapKho.Rows.Count - 1
                dtPhieuNhapKho.Rows(i)("STT") = i + 1
                formatDate2VN(dtPhieuNhapKho.Rows(i)("Ngay"))
                formatDate2VN(dtPhieuNhapKho.Rows(i)("NgayNhapKho"))
            Next
            dgvPhieuNhapKho.DataSource = dtPhieuNhapKho
            If dgvPhieuNhapKho.Rows.Count - 1 > 0 Then
                dgvPhieuNhapKho.Rows(0).Selected = True
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Public Sub Report_TheoSoPhieu()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dsPhieuNhapKho As New DataSet
            Dim dtPhieuNhapKho As New DataTable
            Dim query As String = ""
            If rbtTatCa.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And So='" & txtSoPhieu.Text.Trim & "'"
            ElseIf rbtChuaDuyet.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And TrangThai=N'Chưa duyệt' And So='" & txtSoPhieu.Text.Trim & "'"
            ElseIf rbtDaDuyet.Checked Then
                query = "Select * from PhieuNhapKho Where Xoa=0 And LoaiPhieu=N'HangTraVe' And TrangThai=N'Đã duyệt' And So='" & txtSoPhieu.Text.Trim & "'"
            End If
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dsPhieuNhapKho)
            dbConn.Close()
            dgvPhieuNhapKho.AutoGenerateColumns = False
            dtPhieuNhapKho = dsPhieuNhapKho.Tables(0)
            dtPhieuNhapKho.Columns.Add("STT")
            For i As Integer = 0 To dtPhieuNhapKho.Rows.Count - 1
                dtPhieuNhapKho.Rows(i)("STT") = i + 1
                formatDate2VN(dtPhieuNhapKho.Rows(i)("Ngay"))
                formatDate2VN(dtPhieuNhapKho.Rows(i)("NgayNhapKho"))
            Next
            dgvPhieuNhapKho.DataSource = dtPhieuNhapKho
            If dgvPhieuNhapKho.Rows.Count - 1 > 0 Then
                dgvPhieuNhapKho.Rows(0).Selected = True
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Filter Data"
    Sub DisableControl()
        dtpTheoNgay.Enabled = False
        cbxTheoThang.Enabled = False
        cbxNamTheoThang.Enabled = False
        cbxTheoNam.Enabled = False
        dtpTuNgay.Enabled = False
        dtpDenNgay.Enabled = False
        txtSoPhieu.Enabled = False
    End Sub

    Private Sub rbtTheoNgay_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtTheoNgay.CheckedChanged
        DisableControl()
        dtpTheoNgay.Enabled = True
        ReportType = 0
    End Sub

    Private IsPostBack As Boolean = False
    Private Sub rbtTheoThang_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtTheoThang.CheckedChanged
        DisableControl()
        cbxTheoThang.Enabled = True
        cbxNamTheoThang.Enabled = True
        If IsPostBack = False Then
            cbxTheoThang.SelectedIndex = IndexCurrentMonth
        End If
        cbxNamTheoThang.Enabled = True
        If IsPostBack = False Then
            cbxNamTheoThang.SelectedIndex = 0
        End If
        ReportType = 1
        IsPostBack = True
    End Sub
    Private IsPostBack2 As Boolean = False
    Private Sub rbtTheoNam_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtTheoNam.CheckedChanged
        DisableControl()
        cbxTheoNam.Enabled = True
        If IsPostBack2 = False Then
            cbxTheoNam.SelectedIndex = 0
        End If
        ReportType = 2
        IsPostBack2 = True
    End Sub

    Private Sub rbtTuNgay_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtTuNgay.CheckedChanged
        DisableControl()
        dtpTuNgay.Enabled = True
        dtpDenNgay.Enabled = True
        ReportType = 3
    End Sub

    Private Sub rbtTheoPhieu_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtTheoPhieu.CheckedChanged
        Try
            DisableControl()
            txtSoPhieu.Enabled = True
            ReportType = 4
        Catch ex As Exception

        End Try
    End Sub
#End Region
#Region "Load combo tháng và combo năm"
#Region "Sub Class"
    Private Class YearInfo
        Dim _Nam As Integer
        Dim _TenNam As String
        Public Sub New(ByVal Nam As Integer, ByVal TenNam As String)
            _Nam = Nam
            _TenNam = TenNam
        End Sub
        Public Function Nam() As Integer
            Return _Nam
        End Function
        Public Function TenNam() As String
            Return _TenNam
        End Function
        Public Overrides Function ToString() As String
            Return _TenNam
        End Function
    End Class
    Private Class MonthInfo
        Dim _Thang As Integer
        Dim _TenThang As String
        Public Sub New(ByVal Thang As Integer, ByVal TenThang As String)
            _Thang = Thang
            _TenThang = TenThang
        End Sub
        Public Function Thang() As Integer
            Return _Thang
        End Function
        Public Function TenThang() As String
            Return _TenThang
        End Function
        Public Overrides Function ToString() As String
            Return _TenThang
        End Function
    End Class
#End Region
#Region "Load data"
    Private Sub Nam_Load()
        Dim currentYear As Integer = Now.Year
        cbxTheoNam.Items.Clear()
        Try
            While (currentYear >= 2011)
                cbxTheoNam.Items.Add(New YearInfo(currentYear, currentYear.ToString))
                cbxNamTheoThang.Items.Add(New YearInfo(currentYear, currentYear.ToString))
                currentYear = currentYear - 1
            End While
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Private Sub Thang_Load()
        Dim currentYear As Integer = 1
        cbxTheoThang.Items.Clear()
        Try
            Dim i As Integer = 0
            While (currentYear <= 12)
                cbxTheoThang.Items.Add(New MonthInfo(currentYear, currentYear.ToString))
                If currentYear = Now.Month Then
                    IndexCurrentMonth = i
                End If
                currentYear = currentYear + 1
                i = i + 1
            End While
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
#End Region
#End Region
    Private Sub cmdThongKe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThongKe.Click
        Try
            Select Case ReportType
                Case 0
                    Report_TheoNgay()
                Case 1
                    Report_TheoThang()
                Case 2
                    Report_TheoNam()
                Case 3
                    Report_TuNgayDenNgay()
                Case 4
                    Report_TheoSoPhieu()
            End Select
        Catch ex As Exception

        End Try
    End Sub
    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Try
            Dim PhieuNhapKhoId As Integer = Integer.Parse(dgvPhieuNhapKho.Rows(dgvPhieuNhapKho.CurrentCell.RowIndex).Cells("PhieuNhapKhoId").Value.ToString)
            'kiem tra tinh trang
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim dbCommand As OleDbCommand
            Dim query As String = "Select TrangThai From PhieuNhapKho Where PhieuNhapKhoId=" & PhieuNhapKhoId
            dbCommand = New OleDbCommand(query, dbConn)
            Dim TrangThai As String = dbCommand.ExecuteScalar()
            If TrangThai = "Đã duyệt" Then
                MessageBox.Show("Phiếu đã duyệt.Bạn không thể xóa thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                dbConn.Close()
                Exit Sub
            Else
                Dim dlgRes As DialogResult
                dlgRes = MessageBox.Show( _
                      "Bạn muốn xóa phiếu nhập kho này", _
                      "Xóa loại sản phẩm", _
                MessageBoxButtons.OKCancel, _
                      MessageBoxIcon.Question)
                If dlgRes = DialogResult.OK Then
                    query = "Delete PhieuNhapKho Where PhieuNhapKhoId=" & PhieuNhapKhoId & ";Delete ChiTietPhieuNhapKho Where PhieuNhapKhoId=" & PhieuNhapKhoId
                    dbCommand = New OleDbCommand(query, dbConn)
                    dbCommand.ExecuteNonQuery()
                    dbConn.Close()
                    Select Case ReportType
                        Case 0
                            Report_TheoNgay()
                        Case 1
                            Report_TheoThang()
                        Case 2
                            Report_TheoNam()
                        Case 3
                            Report_TuNgayDenNgay()
                        Case 4
                            Report_TheoSoPhieu()

                    End Select
                End If

            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click
        Try
            Dim PhieuNhapKhoId As Integer = Integer.Parse(dgvPhieuNhapKho.Rows(dgvPhieuNhapKho.CurrentCell.RowIndex).Cells("PhieuNhapKhoId").Value.ToString)
            frmInPhieuNhapKho.PhieuNhapKhoId = PhieuNhapKhoId
            frmInPhieuNhapKho.Show()
        Catch ex As Exception

        End Try
    End Sub
End Class